package com.shandao.shangxiu.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shandao.shangxiu.R;
import com.shandao.shangxiu.interf.BaseFragmentInterface;
import com.shandao.shangxiu.interf.DialogControl;
import com.shandao.shangxiu.util.TDevice;


/**
 * 碎片基类
 */
public abstract class BaseFragment extends Fragment implements
        View.OnClickListener, BaseFragmentInterface {
	/**
	 * LayoutInflater类
	 */
    protected LayoutInflater mInflater; 

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        this.mInflater = inflater;
        View view = null;
        if (getLayoutId() != 0) {
            view = inflater.inflate(getLayoutId(), container, false);
        }
        initView(view);
        initData();
        return view;
    }

    /**
     * 设置布局文件
     * @return 布局文件的Id
     */
    protected abstract int getLayoutId();

    /**
     * 设置返回按钮监听
     * @return 是否拦截监听
     */
    public boolean onBackPressed() {
        return false;
    }

    /**
     * 接续布局文件
     * @return 一个view
     */
    protected View inflateView(int resId) {
        return this.mInflater.inflate(resId, null);
    }

    /**
     * 隐藏对话框
     */
    protected void hideWaitDialog() {
        FragmentActivity activity = getActivity();
        if (activity instanceof DialogControl) {
            ((DialogControl) activity).hideWaitDialog();
        }
    }

    /**
     * 显示一个对话框 默认使用R.string.loading
     * @return 返回一个加载的对话框控件
     */
    protected ProgressDialog showWaitDialog() {
        return showWaitDialog(R.string.loading);
    }

    /**
     * 显示一个对话框 使用自定义的文字显示
     * @return 返回一个加载的对话框控件
     */
    protected ProgressDialog showWaitDialog(int resid) {
        FragmentActivity activity = getActivity();
        if (activity instanceof DialogControl) {
            return ((DialogControl) activity).showWaitDialog(resid);
        }
        return null;
    }

    /**
     * 显示一个对话框
     * @param resid 使用系统中的自定义的文字显示
     * @return 返回一个加载的对话框控件
     */
    protected ProgressDialog showWaitDialog(String resid) {
        FragmentActivity activity = getActivity();
        if (activity instanceof DialogControl) {
            return ((DialogControl) activity).showWaitDialog(resid);
        }
        return null;
    }
    
    /**
     * 判断是否有网络
     * @return true为有网络 false为没有网络 打开设置网络对话框
     */
    protected boolean isPrepartForNet() {
		if (!TDevice.hasInternet()) {
            TDevice.setNetworkMethod(getActivity());
			return false;
		}
		return true;
	}

}
